<?php
	class LoginControl extends Control {
		public function login(){
			//公共部分
			$str=file_get_contents(CONFIG_DIR.'/public.conf');
			eval($str);
			
			if (isset($_SERVER['HTTP_REFERER'])) {
				$_SESSION['page_url']=$_SERVER['HTTP_REFERER'];
			}
			
			$this->view->display('login.tpl');
		}
	
		public function loginck(){
				if(isset($_POST['login'])){
					$username=$_POST['username'];
					$password=md5($_POST['pwd']);
					$member=new MemberModel();
					$userinfo=$member->login($username);
					if($userinfo&&$password==$userinfo['pwd']){
						$_SESSION['identity']=$_SERVER['REMOTE_ADDR'];
						$_SESSION['uid']=$userinfo['id'];
						$_SESSION['username']=$userinfo['username'];
						$_SESSION['relname']=$userinfo['relname'];
						$_SESSION['address']=$userinfo['address'];
						$_SESSION['phone']=$userinfo['phone'];
						$_SESSION['code']=$userinfo['code'];
						$_SESSION['email']=$userinfo['email'];
						
						if(!isset($_SESSION['page_url'])){
							echo 'ok';
							//$this->success('/index.php/Home/Member/index','登录成功');
						}else{
							echo 'okok';
							//$this->success($_SESSION['page_url'],'登录成功');
						}
					}else{
						echo 'fail';
					}
				}else{
					echo 'illlegal';
				}
			}
			
			public function ajaxck(){
				if(isset($_POST['username'])){
					$username=$_POST['username'];
					$member=new MemberModel();
					$res=$member->login($username);
					if($res){
						echo 'yes';
					}else{
						echo 'no';
					}
				}
			}
			
			public function registerck(){
				if($_POST['pwd']==$_POST['rpwd']){
					$member=new MemberModel();
					$data=array(
						'username'	=>$_POST['username'],
						'pwd'		=>md5($_POST['pwd']),
						'email'		=>$_POST['email']
					);
					$num=$member->insert($data);
					if($num==1){
						$this->success('/index.php/Home/Member/index');
					}else{
						$this->success('/index.php/Home/Login/login','注册失败，用户名已经被注册！');
					}
				}
			}
			
			public function loginout(){
				session_destroy();
				if(isset($_SERVER['HTTP_REFERER'])){
					$this->jump($_SERVER['HTTP_REFERER']);
				}else {
					$this->jump('/index.php/Home/Index/index');
				}
			}
	}
?>